//
//  XMComposeToolBarView.swift
//  XMWeimo
//
//  Created by ios001 on 16/7/27.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

// 按钮枚举
enum XMComposeToolBarViewType: Int{
    // 图片
    case Picture = 1001
    // @
    case Mention = 1002
    // #
    case Thrend = 1003
    // 表情
    case Emoticon = 1004
    // +
    case Add = 1005

}

class XMComposeToolBarView: UIView {
    
    // 表情按钮
    var emoticonButton: UIButton?
    // 是否是表情键盘
    var isEmoticon: Bool = false {
        didSet{
            // 如果当前为true 是表情键盘,button显示的image是键盘图片
            // 反之 为false 显示的是系统键盘,button显示的image 是表情图片
            let imgName = isEmoticon ? "compose_keyboardbutton_background" : "compose_emoticonbutton_background"
            emoticonButton?.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
            emoticonButton?.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
        }
    }
    
    // 定义一个闭包
    var closure:((type: XMComposeToolBarViewType)->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 监听方法
    func buttonClick(button: UIButton){
        // 执行闭包
        closure?(type: XMComposeToolBarViewType(rawValue: button.tag)!)
    }
    
    // MARK: - 设置视图
    func setupUI(){
        backgroundColor = UIColor.whiteColor()
        // 添加控件
        let pictureBuutton = addClildButton("compose_toolbar_picture",type: .Picture)
        let mentionBuutton = addClildButton("compose_mentionbutton_background",type: .Mention)
        let trendBuutton = addClildButton("compose_trendbutton_background",type: .Thrend)
        emoticonButton = addClildButton("compose_emoticonbutton_background",type: .Emoticon)
        let addBuutton = addClildButton("compose_add_background",type: .Add)
        
        // 添加约束
        pictureBuutton.snp_makeConstraints { (make) -> Void in
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(mentionBuutton)
        }
        
        mentionBuutton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(pictureBuutton.snp_trailing)
            make.width.equalTo(trendBuutton)
        }
        
        trendBuutton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(mentionBuutton.snp_trailing)
            make.width.equalTo(emoticonButton!)
        }
        
        emoticonButton!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(trendBuutton.snp_trailing)
            make.width.equalTo(addBuutton)
        }
        
        addBuutton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emoticonButton!.snp_trailing)
        }
    }
    
    // MARK: - 抽取出创建button的公共方法
    func addClildButton(imgName: String, type: XMComposeToolBarViewType) ->UIButton{
        let button = UIButton()
        button.tag = type.rawValue
        // 添加点击事件
        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
//        button.backgroundColor = RandomColor()
        addSubview(button)
        return button
    }
}
